package com.wuwei.elearning.question.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwei.elearning.exception.ElearningMysqlException;
import com.wuwei.elearning.question.domain.Audio;
import com.wuwei.elearning.question.domain.AudioQuestion;
import com.wuwei.elearning.question.mapper.AudioMapper;
import com.wuwei.elearning.question.mapper.AudioQuestionMapper;
import com.wuwei.elearning.question.service.AudioService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;

@Service
public class AudioServiceImpl extends ServiceImpl<AudioMapper, Audio> implements AudioService {

    @Resource
    private AudioMapper audioMapper;

    @Override
    public void insert(String fileName, Long account, String uuid, String suffix, String duration) {
        LambdaQueryWrapper<Audio> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Audio::getName,fileName);
        Audio audio = audioMapper.selectOne(queryWrapper);
        if (audio!=null) {
            throw  new ElearningMysqlException(30011,"上传失败！该文件已存在！");
        } else {
            audio = new Audio();
            audio.setDuration(duration);
            audio.setPath(uuid+"."+suffix);
            audio.setCreatedBy(account);
            audio.setCreatedTime(LocalDateTime.now());
            audio.setName(fileName);
            audioMapper.insert(audio);
        }
    }

    @Value("${prop.upload-folder}")
    private String UPLOAD_FOLDER;


    /**
     * 删除数据库中数据
     * 删除硬盘中数据
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        Audio audio = audioMapper.selectById(id);
        if (audio!=null) {
            String path = audio.getPath();
            File file = new File(UPLOAD_FOLDER+path);
            if (file.exists()) {
                if (!file.delete()) {
                    throw new ElearningMysqlException(30033,"文件删除失败");
                }else {
                    audioMapper.deleteById(id);
                }
            } else {
                log.error("删除失败，本地该音频不存在！");
                audioMapper.deleteById(id);
            }
        }else {
            throw new ElearningMysqlException(30011,"删除失败，音频文件的主键id错误！");
        }
    }

    @Resource
    private AudioQuestionMapper audioQuestionMapper;

    /**
     * 更新audio表
     * 更新audioQuestion映射表
     * @param audio
     */
    @Override
    @Transactional
    public void myUpdateById(Audio audio) {
        audioMapper.updateById(audio);

        String answers = audio.getAnswers();
        String questions = audio.getQuestions();
        String name = audio.getName();

        LambdaQueryWrapper<AudioQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AudioQuestion::getFileName,name);
        AudioQuestion audioQuestion = audioQuestionMapper.selectOne(wrapper);
        if (audioQuestion!=null) {
            audioQuestion.setQuestions(questions);
            audioQuestion.setAnswers(answers);
            audioQuestion.setFileName(name);
            audioQuestionMapper.update(audioQuestion,wrapper);
        }
    }
}
